package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    public List<NursingPlanVo> getAllNursingPlan() {

        return nursingPlanMapper.getAllNursingPlan();
    }

    @Override
    public void delete(Long id) {
        nursingPlanMapper.delete(id);
    }

    @Transactional
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlanMapper.addNursingPlan(nursingPlan);

        //完善 添加计划中的项目
        //根据计划名查询计划
        NursingPlan plan = nursingPlanMapper.getNursingPlanByPlanName(nursingPlan.getPlanName());
        List<NursingProjectPlan> nursingProjectPlans = nursingPlanDto.getProjectPlans();
        nursingProjectPlans.forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(plan.getId());
        });
        nursingProjectPlanMapper.insertBatch(nursingProjectPlans);
    }

    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getNursingPlanById(id);
        log.info("响应的护理计划nursingPlanVo={}",nursingPlanVo);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.getNursingProjectPlanByPlanId(id);
        log.info("护理计划中的项目nursingProjectPlans={}",nursingProjectPlans);
        nursingPlanVo.setProjectPlans(nursingProjectPlans);
        log.info("最终响应的护理计划nursingPlanVo={}",nursingPlanVo);

        return nursingPlanVo;
    }

    @Override
    public PageResponse<NursingPlanVo> selectByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        log.info("请求的护理计划name={},status={}",name,status);
        Page<NursingPlanVo> page = nursingPlanMapper.selectByPage(name, status);
        log.info("响应的护理计划page={}",page);
        return PageResponse.of(page, NursingPlanVo.class);
    }

    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        // 设置计划ID
        projectPlans.forEach(projectPlan -> {projectPlan.setPlanId(nursingPlan.getId());});
        //删除计划项目表中该计划的项目
        nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
        //插入新的项目
        nursingProjectPlanMapper.insertBatch(projectPlans);
    }

    @Override
    public void updateNursingPlanStatus(Long id, Integer status) {
        // 更新计划状态
        nursingPlanMapper.updateNursingPlanStatus(id, status);
    }
}

